/**
 * 语言状态管理
 * 使用Pinia管理应用语言状态
 */

import { defineStore } from 'pinia'
import { getCurrentLanguage, changeLanguage, LANGUAGES, t } from '@/utils/i18n'

export const useLanguageStore = defineStore('language', {
  state: () => ({
    currentLanguage: getCurrentLanguage()
  }),
  
  getters: {
    // 获取当前语言代码
    lang: (state) => state.currentLanguage,
    
    // 检查当前是否为中文
    isChinese: (state) => state.currentLanguage === LANGUAGES.ZH,
    
    // 检查当前是否为英文
    isEnglish: (state) => state.currentLanguage === LANGUAGES.EN,
    
    // 获取当前语言名称
    languageName: (state) => {
      return state.currentLanguage === LANGUAGES.ZH ? '中文' : 'English'
    }
  },
  
  actions: {
    // 检查翻译系统状态
    checkTranslationSystem() {
      const testKey = 'dashboard.myMarketing'
      const translation = t(testKey)
      return { 
        currentLanguage: this.currentLanguage,
        localStorage: localStorage.getItem('APP_LANGUAGE'),
        htmlLang: document.documentElement.getAttribute('lang'),
        testTranslation: translation 
      }
    },
    
    // 设置新的语言
    setLanguage(lang) {
      const success = changeLanguage(lang)
      if (success) {
        this.currentLanguage = lang
      }
      return success
    },
    
    // 切换到中文
    switchToChinese() {
      return this.setLanguage(LANGUAGES.ZH)
    },
    
    // 切换到英文
    switchToEnglish() {
      return this.setLanguage(LANGUAGES.EN)
    },
    
    // 切换语言（中英互换）
    toggleLanguage() {
      const newLang = this.currentLanguage === LANGUAGES.ZH ? LANGUAGES.EN : LANGUAGES.ZH
      return this.setLanguage(newLang)
    }
  }
}); 